<?php /* Smarty version 2.6.20, created on 2013-05-27 05:17:55
         compiled from _core/_include/module/_mvc/control/handlecontainerpurchase.tpl */ ?>
<?php require_once(SMARTY_CORE_DIR . 'core.load_plugins.php');
smarty_core_load_plugins(array('plugins' => array(array('modifier', 'escape', '_core/_include/module/_mvc/control/handlecontainerpurchase.tpl', 25, false),)), $this); ?>
function HandleContainerPurchase(gui, item, options) {
  this._gui = gui;
  this._item = item;
  this._options = options;
  
  this._parent = (options && options.parent) ? options.parent : null;
  this._samples = (options && options.samples) ? options.samples : [];
  this._callbackFunc = (options && options.callbackFunc) ? options.callbackFunc : function() {};
  this._noEdit = (options && options.noEdit) ? options.noEdit : null;
  this._hidePrice = (options && options.hidePrice) ? options.hidePrice : null;
  this._isAdd = (options && options.isAdd) ? options.isAdd : false;
  this._isPackage = (options && options.isPackage) ? options.isPackage : false;

  this._price = (this._item.total) ? this._item.total : 0;
  
  this._createElements();
};

HandleContainerPurchase.prototype._createElements = function() {
  var option, _self = this;
  DlmanageUtils.clear(this._gui);
  
  if (this._isPackage) {
    this._albums = [];
    if (this._item.sample.type == '<?php echo ((is_array($_tmp=@SAMPLE_TYPE_ALBUM)) ? $this->_run_mod_handler('escape', true, $_tmp, 'javascript') : smarty_modifier_escape($_tmp, 'javascript')); ?>
' || this._item.sample.type == '<?php echo ((is_array($_tmp=@SAMPLE_TYPE_MAGAZINE)) ? $this->_run_mod_handler('escape', true, $_tmp, 'javascript') : smarty_modifier_escape($_tmp, 'javascript')); ?>
') {
      this._albums[this._albums.length] = this._item;
    }
    for (var i = 0, il = this._parent.packageItems.length; i < il; i++) {
      var packageItem = this._parent.packageItems[i];
      if (packageItem.trackId != this._item.trackId && (packageItem.sample.type == '<?php echo ((is_array($_tmp=@SAMPLE_TYPE_ALBUM)) ? $this->_run_mod_handler('escape', true, $_tmp, 'javascript') : smarty_modifier_escape($_tmp, 'javascript')); ?>
' || packageItem.sample.type == '<?php echo ((is_array($_tmp=@SAMPLE_TYPE_MAGAZINE)) ? $this->_run_mod_handler('escape', true, $_tmp, 'javascript') : smarty_modifier_escape($_tmp, 'javascript')); ?>
')) {
        this._albums[this._albums.length] = packageItem;
      }
    }
    MiscUtils.sort(this._albums, ['otherId']);
  
    this._designs = {};
    for (var i = 0, il = this._albums.length; i < il; i++) {
      var album = this._albums[i];
      
      if (this._designs[album.otherId] == undefined) {
        this._designs[album.otherId] = [album];
      } else {
        this._designs[album.otherId][this._designs[album.otherId].length] = album;
      }
    }
    
    MiscUtils.sort(this._samples, ['name']);
    for (var i = 0, il = this._samples.length; i < il; i++) {
      var sample = this._samples[i];
      option = new Option(sample.name + ' ($' + sample.price + ')');
      option._sample = sample;
      this._gui.sample.options[this._gui.sample.options.length] = option;
      if (this._item.sample.id == sample.id) {
        this._gui.sample.selectedIndex = this._gui.sample.length - 1;
      }
    }
    
    this._gui.quantity.value = this._item.quantity;
    this._gui.price.value = this._item.sample.price * this._item.quantity;
    var sample = this._item.sample;
    if (sample) {
      if (this._item.selects.length == 0) {
        for (var m = 0, ml = sample.attributes.length; m < ml; m++) {
          var attribute = sample.attributes[m];
          DlmanageUtils.packageChoose(this._item, attribute, attribute.options[0]);
        }
      }
      
      for (var i = 0, il = sample.attributes.length; i < il; i++) {
        var attribute = sample.attributes[i];
        var gattribute = this._gui.attributes[i];
        gattribute.name.appendChild(document.createTextNode(attribute.name));
        for (var j = 0, jl = attribute.options.length; j < jl; j++) {
          var option = attribute.options[j];
          var goption = gattribute.options[j];
          goption.name.style.color = '#000000';
          goption.name.appendChild(document.createTextNode(option.name));
          goption.name._attribute = attribute;
          goption.name._option = option;
          goption.name.onclick = function() { DlmanageUtils.packageChoose(_self._item, this._attribute, this._option);
                                              _self._createElements.call(_self);
                                            };
          if (DlmanageUtils.chosen(this._item, attribute, option)) {
            goption.name.style.color = '#ff0000';
          }
        }
      }
    }
    if (this._gui.design) {
      if (this._designs[this._item.otherId]) {
        var table, td, tr;
        table = document.createElement('table');
        this._gui.design.appendChild(table);
        
        if (this._designs[this._item.otherId].length > 1) {
          var button = DOMUtils.createInput(null, null, 'button', 'Dedicated Design', null);
          button.onclick = function() { _self._item.otherId = MiscUtils.unique();
                                        _self._createElements.call(_self);
                                      };
          tr = table.insertRow(-1);
          td = tr.insertCell(-1);
          td.appendChild(button);
        }
      
        if (this._designs[this._item.otherId].length == 1) {
          var radio = DOMUtils.createInput(null, null, 'radio', null, null);
          radio.checked = true;
          tr = table.insertRow(-1);
          td = tr.insertCell(-1);
          td.appendChild(radio);
          var other = DlmanageUtils.congregate1(this._designs[this._item.otherId]);
          td.appendChild(document.createTextNode('Dedicated Design'));
        }
        
        for (var otherId in this._designs) {
          var other = DlmanageUtils.congregate1(this._designs[otherId]);
          var self = DlmanageUtils.congregate1([this._item]);
          
          if (other.page < self.page || other.shape != self.shape || (this._item.otherId == otherId && this._designs[this._item.otherId].length == 1)) {
            continue;
          }
          
          var radio = DOMUtils.createInput(null, null, 'radio', null, null);
          radio._otherId = otherId;
          radio.checked = (this._item.otherId == otherId);
          radio.onclick = function() { _self._item.otherId = this._otherId;
                                       _self._createElements.call(_self);
                                     };
          tr = table.insertRow(-1);
          td = tr.insertCell(-1);
          td.appendChild(radio);
          td.appendChild(document.createTextNode((this._item.otherId == otherId && this._designs[this._item.otherId].length == 1) ? 'Dedicated Design' : other.title));
        }
      }
    }
    
    if ((this._item.sample.type == Constant.SAMPLE_TYPE_VIDEO && this._item.sample.model == Constant.MODEL_NAME_PRODUCT_VIDEO) || this._item.sample.type == Constant.SAMPLE_TYPE_MAGAZINE || this._item.sample.type == Constant.SAMPLE_TYPE_ALBUM) {
      this._gui.quantity.disabled = true;
    } else {
      this._gui.quantity.disabled = false;
    }
    
    this._gui.sample.onchange = function() { _self._item.sample = this.options[this.selectedIndex]._sample;
                                             _self._item.quantity = 1;
                                             _self._item.total = this.options[this.selectedIndex]._sample.price;
                                             _self._item.otherId = MiscUtils.unique();
                                             _self._item.selects = [];
                                             for (var i = 0, il = _self._item.sample.attributes.length; i < il; i++) {
                                               var attribute = _self._item.sample.attributes[i];
                                               DlmanageUtils.packageChoose(_self._item, attribute, attribute.options[0]);
                                             }
                                             _self._callbackFunc();
                                           };
    this._gui.quantity.onchange = function() { _self._item.quantity = this.value;
                                               _self._createElements.call(_self);
                                             };
                                             
    this._gui.price.disabled = true;
    
    if (!this._noEdit) {
      this._gui.gst.appendChild(document.createTextNode((this._item.sample.price * this._item.quantity > 0) ? ' (Inc. ' + '<?php echo ((is_array($_tmp=@TAX)) ? $this->_run_mod_handler('escape', true, $_tmp, 'javascript') : smarty_modifier_escape($_tmp, 'javascript')); ?>
' + ' $' + (this._item.sample.price * this._item.quantity * parseFloat(1 + parseFloat('<?php echo ((is_array($_tmp=@GST)) ? $this->_run_mod_handler('escape', true, $_tmp, 'javascript') : smarty_modifier_escape($_tmp, 'javascript')); ?>
'), 10)).toFixed(2) + ') ' : ''));
    } else {
      this._gui.gst.appendChild(document.createTextNode(''));
    }
    
    if (!this._isAdd) {
      this._gui.product.style.padding = '0';
      this._gui.product.style.fontSize = '13px';
      this._gui.product.style.fontWeight = 'bold';
      this._gui.product.appendChild(document.createTextNode(this._item.sample.name));
      this._gui.sample.style.display = 'none';
    } else {
      this._gui.sample.style.display = 'inline';
      this._gui.product.appendChild(this._gui.sample);
    }
    
  } else {
    
    this._albums = [];
    if (this._item.type == '<?php echo ((is_array($_tmp=@SAMPLE_TYPE_ALBUM)) ? $this->_run_mod_handler('escape', true, $_tmp, 'javascript') : smarty_modifier_escape($_tmp, 'javascript')); ?>
' || this._item.type == '<?php echo ((is_array($_tmp=@SAMPLE_TYPE_MAGAZINE)) ? $this->_run_mod_handler('escape', true, $_tmp, 'javascript') : smarty_modifier_escape($_tmp, 'javascript')); ?>
') {
      this._albums[this._albums.length] = this._item;
    }
    for (var i = 0, il = this._parent.purchaseItems.length; i < il; i++) {
      var purchase = this._parent.purchaseItems[i];
      if (purchase.cancelled == 1) {
        continue;
      }
      if (purchase.trackId != this._item.trackId && (purchase.type == '<?php echo ((is_array($_tmp=@SAMPLE_TYPE_ALBUM)) ? $this->_run_mod_handler('escape', true, $_tmp, 'javascript') : smarty_modifier_escape($_tmp, 'javascript')); ?>
' || purchase.type == '<?php echo ((is_array($_tmp=@SAMPLE_TYPE_MAGAZINE)) ? $this->_run_mod_handler('escape', true, $_tmp, 'javascript') : smarty_modifier_escape($_tmp, 'javascript')); ?>
')) {
        this._albums[this._albums.length] = purchase;
      }
    }
    MiscUtils.sort(this._albums, ['otherId']);
  
    this._designs = {};
    for (var i = 0, il = this._albums.length; i < il; i++) {
      var album = this._albums[i];
      
      if (this._designs[album.otherId] == undefined) {
        this._designs[album.otherId] = [album];
      } else {
        this._designs[album.otherId][this._designs[album.otherId].length] = album;
      }
    }
    
    MiscUtils.sort(this._samples, ['name']);
    for (var i = 0, il = this._samples.length; i < il; i++) {
      var sample = this._samples[i];
      option = new Option(sample.name + ' ($' + sample.price + ')');
      option._sample = sample;
      this._gui.sample.options[this._gui.sample.options.length] = option;
      if (this._item.sampleId == sample.id) {
        this._gui.sample.selectedIndex = this._gui.sample.length - 1;
      }
    }
    
    this._gui.quantity.value = this._item.quantity;
    this._gui.price.value = this._item.total;
    
    var sample = DlmanageUtils.sample(this._item, this._options.samples);
    if (sample) {
      if (this._item.selects.length == 0) {
        for (var m = 0, ml = sample.attributes.length; m < ml; m++) {
          var attribute = sample.attributes[m];
          DlmanageUtils.purchaseChoose(this._item, attribute, attribute.options[0]);
        }
      }
      for (var i = 0, il = sample.attributes.length; i < il; i++) {
        var attribute = sample.attributes[i];
        var gattribute = this._gui.attributes[i];
        gattribute.name.appendChild(document.createTextNode(attribute.name));
        for (var j = 0, jl = attribute.options.length; j < jl; j++) {
          var option = attribute.options[j];
          var goption = gattribute.options[j];
          goption.name.style.color = '#000000';
          goption.name.appendChild(document.createTextNode(option.name));
          goption.name._attribute = attribute;
          goption.name._option = option;
          goption.name.onclick = function() { DlmanageUtils.purchaseChoose(_self._item, this._attribute, this._option);
                                              _self._createElements.call(_self);
                                            };
          if (DlmanageUtils.chosen(this._item, attribute, option)) {
            goption.name.style.color = '#ff0000';
          }
        }
      }
    } else {
      for (var i = 0, il = this._item.selects.length; i < il; i++) {
        var select = this._item.selects[i];
        var gattribute = this._gui.attributes[i];
        gattribute.name.appendChild(document.createTextNode(select.title));
        gattribute.option.style.color = '#000000';
        gattribute.option.appendChild(document.createTextNode(select.value));
        gattribute.option.style.color = '#ff0000';
      }
    }
    
    if (this._gui.design) {
      if (this._designs[this._item.otherId]) {
        var table, td, tr;
        table = document.createElement('table');
        this._gui.design.appendChild(table);
        
        if (this._designs[this._item.otherId].length > 1) {
          var button = DOMUtils.createInput(null, null, 'button', 'Dedicated Design', null);
          button.onclick = function() { _self._item.otherId = MiscUtils.unique();
                                        _self._item.isFree = true;
                                        _self._item.total = (_self._item.isFree) ? parseFloat(_self._item.quantity * _self._item.price) + parseFloat(180) : _self._price;
                                        _self._createElements.call(_self);
                                      };
          tr = table.insertRow(-1);
          td = tr.insertCell(-1);
          td.appendChild(button);
        }
      
        if (this._designs[this._item.otherId].length == 1) {
          var radio = DOMUtils.createInput(null, null, 'radio', null, null);
          radio.checked = true;
          tr = table.insertRow(-1);
          td = tr.insertCell(-1);
          td.appendChild(radio);
          var other = DlmanageUtils.congregate2(this._designs[this._item.otherId]);
          td.appendChild(document.createTextNode('Dedicated Design' + ((this._item.isFree) ? ' (+$180.00)' : ' (free)')));
          if (this._noEdit) {
            radio.disabled = true;
          }
        }
        
        for (var otherId in this._designs) {
          var other = DlmanageUtils.congregate2(this._designs[otherId]);
          var self = DlmanageUtils.congregate2([this._item]);
          
          if (other.page < self.page || other.shape != self.shape || (this._item.otherId == otherId && this._designs[this._item.otherId].length == 1)) {
            continue;
          }
          
          var radio = DOMUtils.createInput(null, null, 'radio', null, null);
          radio._otherId = otherId;
          radio.checked = (this._item.otherId == otherId);
          radio.onclick = function() { _self._item.otherId = this._otherId;
                                       _self._item.isFree = false;
                                       _self._item.total = _self._item.price * _self._item.quantity;
                                       _self._createElements.call(_self);
                                     };
          tr = table.insertRow(-1);
          td = tr.insertCell(-1);
          td.appendChild(radio);
          td.appendChild(document.createTextNode((this._item.otherId == otherId && this._designs[this._item.otherId].length == 1) ? 'Dedicated Design (+$180.00)' : other.title + ' (free)'));
          if (this._noEdit) {
            radio.disabled = true;
          }
        }
      }
    }
    
    if ((this._item.type == Constant.SAMPLE_TYPE_VIDEO && this._item.model == Constant.MODEL_NAME_PRODUCT_VIDEO) || this._item.type == Constant.SAMPLE_TYPE_MAGAZINE || this._item.type == Constant.SAMPLE_TYPE_ALBUM) {
      this._gui.quantity.disabled = true;
    } else {
      this._gui.quantity.disabled = false;
    }
    
    this._gui.sample.onchange = function() { var sample = this.options[this.selectedIndex]._sample;
                                             _self._item.sampleId = sample.id;
                                             _self._item.name = sample.name;
                                             _self._item.price = sample.price;
                                             _self._item.type = sample.type;
                                             _self._item.unit = sample.unit;
                                             _self._item.model = sample.model;
                                             _self._item.otherId = MiscUtils.unique();
                                             _self._item.selects = [];
                                              for (var i = 0, il = sample.attributes.length; i < il; i++) {
                                                var attribute = sample.attributes[i];
                                                DlmanageUtils.purchaseChoose(_self._item, attribute, attribute.options[0]);
                                              }
                                             _self._item.quantity = 1;
                                             _self._item.isFree = (DlmanageUtils.isFirstAlbum(_self._parent.purchaseItems, _self._item)) ? false : true;
                                             _self._item.total = parseFloat(this.options[this.selectedIndex]._sample.price) + ((_self._item.isFree && (_self._item.type == '<?php echo ((is_array($_tmp=@SAMPLE_TYPE_ALBUM)) ? $this->_run_mod_handler('escape', true, $_tmp, 'javascript') : smarty_modifier_escape($_tmp, 'javascript')); ?>
' || _self._item.type == '<?php echo ((is_array($_tmp=@SAMPLE_TYPE_MAGAZINE)) ? $this->_run_mod_handler('escape', true, $_tmp, 'javascript') : smarty_modifier_escape($_tmp, 'javascript')); ?>
')) ? parseFloat(180) : parseFloat(0));
                                             _self._callbackFunc();
                                           };
    this._gui.quantity.onchange = function() { _self._item.quantity = this.value;
                                               _self._item.total = parseFloat(_self._item.quantity * _self._item.price) + ((_self._item.isFree && (_self._item.type == '<?php echo ((is_array($_tmp=@SAMPLE_TYPE_ALBUM)) ? $this->_run_mod_handler('escape', true, $_tmp, 'javascript') : smarty_modifier_escape($_tmp, 'javascript')); ?>
' || _self._item.type == '<?php echo ((is_array($_tmp=@SAMPLE_TYPE_MAGAZINE)) ? $this->_run_mod_handler('escape', true, $_tmp, 'javascript') : smarty_modifier_escape($_tmp, 'javascript')); ?>
')) ? parseFloat(180) : parseFloat(0));
                                               _self._createElements.call(_self);
                                             };
    this._gui.price.onchange = function() { if (this.value.replace(/\s/g, "") == '' || isNaN(this.value)) {
                                              this.value = 0;
                                            }
                                            _self._item.total = this.value;
                                            _self._createElements.call(_self);
                                          };
    
    if (!this._noEdit) {
      this._gui.gst.appendChild(document.createTextNode((_self._item.total > 0) ? ' (Inc. ' + '<?php echo ((is_array($_tmp=@TAX)) ? $this->_run_mod_handler('escape', true, $_tmp, 'javascript') : smarty_modifier_escape($_tmp, 'javascript')); ?>
' + ' $' + (_self._item.total * parseFloat(1 + parseFloat('<?php echo ((is_array($_tmp=@GST)) ? $this->_run_mod_handler('escape', true, $_tmp, 'javascript') : smarty_modifier_escape($_tmp, 'javascript')); ?>
'), 10)).toFixed(2) + ') ' : ''));
    } else {
      this._gui.gst.appendChild(document.createTextNode(''));
    }
    
    if (this._noEdit) {
      this._gui.quantity.disabled = true;
      this._gui.price.disabled = true;
      this._gui.price.value = '-';
    }
    if (!this._isAdd) {
      this._gui.product.style.padding = '0';
      this._gui.product.style.fontSize = '13px';
      this._gui.product.style.fontWeight = 'bold';
      this._gui.product.appendChild(document.createTextNode(this._item.name));
      this._gui.sample.style.display = 'none';
    } else {
      this._gui.sample.style.display = 'inline';
      this._gui.product.appendChild(this._gui.sample);
    }
  }
};